package com.czkt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czkt.entity.vo.OrdersLineAndOrders;
import com.czkt.entity.vo.OrdersLineProduct;
import com.czkt.service.ContrRptService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ContrRptController {
    @Resource
    ContrRptService contrRptService;

    @RequestMapping("/contrRpt/list")
    public String contrRptView(@RequestParam(defaultValue = "") String odrCustomer,
                               String originDate,
                               String finishDate,
                               @RequestParam(defaultValue = "1") Integer pageIndex,
                               @RequestParam(defaultValue = "2") Integer pageSize,
                               Model model){
        IPage<OrdersLineProduct> page = contrRptService.ordersLineAndProduct(odrCustomer,originDate,finishDate,pageIndex,pageSize);
        model.addAttribute("page",page);
        model.addAttribute("odrCustomer",odrCustomer);
        model.addAttribute("originDate",originDate);
        model.addAttribute("finishDate",finishDate);
        List<OrdersLineAndOrders> ordersLineByCustomer = contrRptService.ordersLineByCustomer();

        String[] str = ordersLineByCustomer.stream()
                .map(OrdersLineAndOrders::getOdrCustomer)
                .toArray(String[]::new);

        model.addAttribute("cusName",str);

        List<OrdersLineAndOrders> ordersLineByDate = contrRptService.ordersLineByDate();

        Date[] str2 = ordersLineByDate.stream()
                .map(OrdersLineAndOrders::getOdrDate)
                .toArray(Date[]::new);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

        String[] strings = new String[str2.length];

        for (int i = 0; i < str2.length; i++) {
            strings[i] = sdf.format(str2[i]);
        }
        model.addAttribute("dateTime",strings);

        List<OrdersLineAndOrders> ordersLineByYear = contrRptService.ordersLineByYear();

        BigDecimal[] strOld = ordersLineByYear.stream()
                .map(OrdersLineAndOrders::getOddPrice)
                .toArray(BigDecimal[]::new);

        String[] oldName = ordersLineByYear.stream()
                .map(OrdersLineAndOrders::getOdrCustomer)
                .toArray(String[]::new);

        model.addAttribute("oldName",oldName);
        model.addAttribute("old",strOld);

        List<OrdersLineAndOrders> ordersLineAndOrders = contrRptService.ordersLineByOrdersYear();

        BigDecimal[] strNew = ordersLineAndOrders.stream()
                .map(OrdersLineAndOrders::getOddPrice)
                .toArray(BigDecimal[]::new);

        String[] strNewName = ordersLineAndOrders.stream()
                .map(OrdersLineAndOrders::getOdrCustomer)
                .toArray(String[]::new);

        model.addAttribute("strNewName",strNewName);
        model.addAttribute("xin",strNew);
        return "/contrRpt/list";
    }
}
